package com.example.literj.util;

import android.util.Log;

import androidx.annotation.NonNull;

import com.example.literj.kt.android.schedulers.AndroidSchedulers;
import com.example.literj.kt.core.Observable;
import com.example.literj.kt.core.ObservableEmitter;
import com.example.literj.kt.core.ObservableOnSubscribe;
import com.example.literj.kt.core.Observer;
import com.example.literj.kt.core.Scheduler;
import com.example.literj.kt.core.Single;
import com.example.literj.kt.core.SingleObserver;
import com.example.literj.kt.disposables.Disposable;
import com.example.literj.kt.schedulers.Schedulers;

public class RxJavaTestUtil {
    private static final String TAG = RxJavaTestUtil.class.getSimpleName();

    private static ObservableOnSubscribe<Integer> getObservableOnSubscribe(String tag) {
        return new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) {
                Log.d(TAG + " " + tag, "subscribe");
                if (emitter != null) {
                    emitter.onNext(1);
                    emitter.onNext(6);
                    emitter.onComplete();
                }
            }
        };
    }

    private static Observer<Integer> getObserver(String tag) {
        return new Observer<Integer>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                Log.d(TAG + " " + tag, "onSubscribe");
            }

            @Override
            public void onNext(Integer value) {
                Log.d(TAG + " " + tag, "onNext = " + value);
            }

            @Override
            public void onError(@NonNull Throwable error) {
                Log.d(TAG + " " + tag, "onError");
                error.printStackTrace();
            }

            @Override
            public void onComplete() {
                Log.d(TAG + " " + tag, "onComplete");
            }
        };
    }

    private static SingleObserver<Integer> getSingleObserver(String tag) {
        return new SingleObserver<Integer>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                Log.d(TAG + " " + tag, "onSubscribe");
            }

            @Override
            public void onSuccess(Integer integer) {
                Log.d(TAG + " " + tag, "onSuccess = " + integer);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                Log.d(TAG + " " + tag, "onError");
                e.printStackTrace();
            }
        };
    }

    public static void sample1() {
        String tag = "sample1";
        Log.d(TAG + " " + tag, "start");
        Observable.create(getObservableOnSubscribe(tag))
                .subscribe(getObserver(tag));
    }

    public static void sample2() {
        String tag = "sample2";
        Log.d(TAG + " " + tag, "start");
        Observable.just(777).subscribe(getObserver(tag));
    }

    public static void sample3() {
        String tag = "sample3";
        Log.d(TAG + " " + tag, "start");
        Single.just(777).map(data1 -> 777).map(data2 -> 888).subscribe(getSingleObserver(tag));
    }

    public static void sample4() {
        String tag = "sample4";
        Log.d(TAG + " " + tag, "start");
        Single.just(777)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(getSingleObserver(tag));
    }
}